package com.example.mystore.mapper;

import com.example.mystore.model.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {

    @Insert("INSERT INTO commodity (name, product_id, parts_id, icon_url, sub_images, detail, spec_param, price, stock, status, is_hot, created, updated) " +
            "VALUES (#{name}, #{productId}, #{partsId}, #{iconUrl}, #{subImages}, #{detail}, #{specParam}, #{price}, #{stock}, #{status}, #{isHot}, #{created}, #{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertProduct(Product product);

    @Update("UPDATE commodity SET name=#{name}, product_id=#{productId}, parts_id=#{partsId}, icon_url=#{iconUrl}, sub_images=#{subImages}, detail=#{detail}, " +
            "spec_param=#{specParam}, price=#{price}, stock=#{stock}, status=#{status}, is_hot=#{isHot}, created=#{created}, updated=#{updated} WHERE id=#{id}")
    void updateProduct(Product product);

    @Select("SELECT id, name, product_id, parts_id, icon_url, sub_images, detail, spec_param, price, stock, status, is_hot, created, updated " +
            "FROM commodity WHERE id = #{id}")
    Product selectProductById(int id);

    @Select("SELECT id, name, product_id, parts_id, icon_url, sub_images, detail, spec_param, price, stock, status, is_hot, created, updated " +
            "FROM commodity")
    List<Product> selectAllProducts();


}
